//
//  BestViewController.m
//  DuBai
//
//  Created by darktm on 14-2-23.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "BestViewController.h"

#import "BestCollectionCell.h"

#import "TaskWithGetGoodsByNew.h"
#import "Best.h"
#import "TaskWithGetGoodsByHot.h"

#import "UIImageView+WebCache.h"


#import "CommodityDetailViewController.h"

@interface BestViewController ()<WYHttpTaskDelegate>

@property(nonatomic,strong)NSMutableArray *bestNewArray;
@property(nonatomic,strong)NSMutableArray *bestHotArray;


@end

@implementation BestViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self.bestNewBtn setImage:[UIImage imageNamed:@"best_btn_new_selected.png"] forState:UIControlStateSelected];
    [self.bestNewBtn setImage:[UIImage imageNamed:@"best_btn_new_normal.png"] forState:UIControlStateNormal];
    self.bestNewBtn.selected = YES;
    
    [self.hotBtn setImage:[UIImage imageNamed:@"best_btn_hot_selected.png"] forState:UIControlStateSelected];
    [self.hotBtn setImage:[UIImage imageNamed:@"best_btn_hot_normal.png"] forState:UIControlStateNormal];
    
    
    [self.cv registerClass:[BestCollectionCell class] forCellWithReuseIdentifier:@"my_cell"];
    [self loadBesWithType:1];
}



#pragma mark - UICollectionView delegate -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if(self.hotBtn.selected){
        int count = ceilf(self.bestHotArray.count / 2.0f);
        
        if(section == count - 1)
            return self.bestNewArray.count - 2 * section;
        else
            return 2;
    }
    
    int count = ceilf(self.bestNewArray.count / 2.0f);
    if(section == count - 1)
        return self.bestNewArray.count - 2 * section;
    else
        return 2;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    BestCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BestCollectionCell" forIndexPath:indexPath];
    cell.layer.borderWidth = 1;
    cell.layer.borderColor = [[[UIColor brownColor] colorWithAlphaComponent:.2] CGColor];
    
    int index = indexPath.section * 2 + indexPath.item;
    
    Best *b = nil;
    
    if(self.hotBtn.selected && self.bestHotArray.count > index){
        b = self.bestHotArray[index];
        if(b.icon)
            [cell.image setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:b.icon]]];
        cell.desc.text = b.desc;
        cell.hots.text = b.hotnum;
        cell.price.text = b.price;
        cell.comments.text = b.selling;
    }else if(self.bestNewBtn.selected && self.bestNewArray.count > index){
     
        b = self.bestNewArray[index];
        if(b.icon)
            [cell.image setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:b.icon]]];
        cell.desc.text = b.desc;
        cell.hots.text = b.hotnum;
        cell.price.text = b.price;
        cell.comments.text = b.selling;
    }
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    if(self.hotBtn.selected){
        int count = ceilf(self.bestHotArray.count / 2.0f);
        return count;
    }
    
    int count = ceilf(self.bestNewArray.count / 2.0f);
    return count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    Best *b = nil;
    int index = indexPath.section * 2 + indexPath.item;
    if(self.hotBtn.selected && self.bestHotArray.count > index){
        b = self.bestHotArray[index];
        CommodityDetailViewController *detail = (CommodityDetailViewController*)[Utils storyBoardWithId:@"CommodityDetailViewController"];
        detail.goodid = b.id;
        [self.navigationController pushViewController:detail animated:YES];
    }else if(self.bestNewBtn.selected && self.bestNewArray.count > index){
        b = self.bestNewArray[index];
        CommodityDetailViewController *detail = (CommodityDetailViewController*)[Utils storyBoardWithId:@"CommodityDetailViewController"];
        detail.goodid = b.id;
        [self.navigationController pushViewController:detail animated:YES];
    }else{
        CommodityDetailViewController *detail = (CommodityDetailViewController*)[Utils storyBoardWithId:@"CommodityDetailViewController"];
        detail.goodid = @"00";
        [self.navigationController pushViewController:detail animated:YES];
    }
}

#pragma mark - http delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    id rsult = [obj parese];

    if([obj isKindOfClass:[TaskWithGetGoodsByHot class]]){
        
        if(self.bestHotArray){
            [self.bestHotArray addObjectsFromArray:rsult];
        }else{
            self.bestHotArray = rsult;
        }
    }else if ([obj isKindOfClass:[TaskWithGetGoodsByNew class]]){
        
        if(self.bestNewArray){
            [self.bestNewArray addObjectsFromArray:rsult];
        }else{
            self.bestNewArray = rsult;
        }
    }
    
    [self.cv reloadData];
}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    DLog(@"%@",obj.networkOp.url);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - private method -

-(void)loadBesWithType:(int)type{
    if(type){//new
        TaskWithGetGoodsByNew *new = [[TaskWithGetGoodsByNew alloc] init];
        if(self.bestNewArray.count){
            Best *best = [self.bestNewArray lastObject];
            new.shelfTime = best.updatetime;
        }
        new.sort = @"-1";
        new.delegate = self;
        [new start];
    }else{//hot
        TaskWithGetGoodsByHot *hot = [[TaskWithGetGoodsByHot alloc] init];
        hot.delegate = self;
        [hot start];
    }
}

#pragma mark - btn actions -

- (IBAction)on_btn_hot:(id)sender {
    if(self.hotBtn.selected) return;
    self.hotBtn.selected = YES;
    self.bestNewBtn.selected = NO;
    
    if(!self.bestHotArray.count)
        [self loadBesWithType:0];
    
    
    [self.cv reloadData];
}

- (IBAction)on_btn_new:(id)sender {
    if(self.bestNewBtn.selected) return;
    self.hotBtn.selected = NO;
    self.bestNewBtn.selected = YES;
    
    
    [self.cv reloadData];
}
@end
